/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef MSDP_SPATIAL_AWARENESS_EPOCH_H
#define MSDP_SPATIAL_AWARENESS_EPOCH_H

#include "spatial_coordinate_system.h"
#include "spatial_communicator.h"
#include "spatial_mgr_event_handler.h"
#include "spatial_awareness_common.h"

namespace OHOS {
namespace Msdp {
class SpatialAwarenessEpoch {
public:
    enum {
        EPOCH_MASTER = 0,
        EPOCH_SLAVE,
    };
    enum {
        EVENT_EPOCH_BASE = EVENT_SUB_MODULE_BASE,
        EVENT_EPOCH_MASTER_START,
        EVENT_EPOCH_MASTER_END,
        EVENT_EPOCH_MASTER_DEMOTE,
        EVENT_EPOCH_SLAVE_START,
        EVENT_EPOCH_SLAVE_END,
        EVENT_EPOCH_MASTER_AGAIN,
        EVENT_EPOCH_INNER_BASE = 3100,
    };
    SpatialAwarenessEpoch(std::shared_ptr<SpatialMgrEventHandler> handler,
        std::shared_ptr<SpatialCommunicator> communicator,
        std::shared_ptr<SpatialCoordinateSystem> coordinateSystem)
        : handler_(handler), communicator_(communicator), coordinateSystem_(coordinateSystem) {}
    virtual ~SpatialAwarenessEpoch() = default;
    virtual void Start() = 0;
    virtual uint32_t GetEpoch() = 0;
    virtual bool HandleEvent(int32_t event, int64_t param) = 0;
    bool IsAlive()
    {
        return alive_;
    };
protected:
    virtual void End() = 0;
    void SendEvent(int32_t event, int64_t param = 0, int64_t delayTime = 0)
    {
        SPATIAL_LOGI(MODULE_SERVICE, "SpatialAwarenessEpoch SendEvent: %{public}d", event);
        handler_->SendEvent(event, param, delayTime);
    };
    void RemoveEvent(int32_t event)
    {
        SPATIAL_LOGI(MODULE_SERVICE, "SpatialAwarenessEpoch RemoveEvent: %{public}d", event);
        handler_->RemoveEvent(event);
    };
    std::shared_ptr<SpatialMgrEventHandler> handler_;
    std::shared_ptr<SpatialCommunicator> communicator_;
    std::shared_ptr<SpatialCoordinateSystem> coordinateSystem_;
    bool alive_;
};
} // namespace Msdp
} // namespace OHOS

#endif // MSDP_SPATIAL_AWARENESS_EPOCH_H